package com.cjh.controller;


import com.cjh.annotation.WebLog;
import com.cjh.feign.OrderFeign;
import com.cjh.feign.UserFeign;
import com.cjh.pojo.Admin;
import com.cjh.pojo.Menu;
import com.cjh.pojo.Order;
import com.cjh.pojo.User;
import com.cjh.vo.ResultMap;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/orderApi")
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;

    @PostMapping("/insertOrder")
    @WebLog(description = "请求=>添加菜品订单接口")
    public ResultMap insertOrder(@RequestParam(value = "menuId",required = false) Integer menuId,
                                 HttpSession session){
        User user = (User) session.getAttribute("user");
        //设置订单信息
        Order order=new Order();
        order.setUser(new User().setId(user.getId()));
        order.setMenu(new Menu().setId(menuId));
        order.setDate(new Date());
        //保存订单
        orderFeign.insertOrder(order);
        return new ResultMap<String>(200,"添加菜品订单","已订购",1);
    }

    @GetMapping("/userOrderList")
    @WebLog(description = "请求=>用户订单信息接口")
    public ResultMap userOrderList(
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                HttpSession session) {

        User user = (User) session.getAttribute("user");
        PageInfo<Order> pageInfo = orderFeign.selectOrdersByUserId(user.getId(), page, limit);
        return new ResultMap<List<Order>>(0, "用户订单信息->每页10条数据", pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @DeleteMapping("/deleteOrder")
    @WebLog(description = "请求=>删除用户订单接口")
    public ResultMap deleteOrder(@RequestParam(value = "id",required = false) Integer id) {

        orderFeign.deleteOrder(id);
        return new ResultMap<String>(200, "删除用户订单", "删除成功",1);
    }

    /**
     * 未处理的订单
     * @param page
     * @param limit
     * @param session
     * @return
     */
    @GetMapping("/untreatedOrderList")
    @WebLog(description = "请求=>所有未派送的订单信息接口")
    public ResultMap untreatedOrderList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            HttpSession session) {

        PageInfo<Order> pageInfo = orderFeign.selectUntreatedOrders(page,limit);
        return new ResultMap<List<Order>>(0, "所有未派送的订单信息->每页10条数据", pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @PutMapping("/updateOrderState")
    @WebLog(description = "请求=>处理用户订单接口")
    public ResultMap updateOrderState(@RequestParam(value = "id",required = false) Integer id,
                                    HttpSession session) {

        Admin admin = (Admin) session.getAttribute("admin");
        orderFeign.updateOrderState(admin.getId(),id);
        return new ResultMap<String>(200, "处理用户订单", "派送成功",1);
    }

    @GetMapping("/getOrderNumber")
    @WebLog(description = "请求=>查询用户订单数量接口")
    public ResultMap getOrderNumber(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer orderNumber = orderFeign.getOrderNumber(user.getId());

        return new ResultMap<Integer>(200,"查询用户订单数量",orderNumber);
    }

}
